import { request } from '@/utils/request';

import { AddParams, MenuInfo, MenuItem, MenuParams } from './model';

/**
 * 查询菜单的树结构目录
 * @param params
 * @returns
 */
export function getMenu(params: MenuParams) {
  return request.get<MenuItem[]>({
    url: `/platform/menus`,
    params,
  });
}

/**
 * 删除菜单
 * @param params 删除id集合
 * @returns
 */
export function menuRemoves(params: string[]) {
  return request.delete({
    url: `/platform/menuRemoves`,
    data: params,
  });
}

/**
 * 菜单降级
 * @param params
 * @returns
 */
export function menuMoveDowngrade(params: { id: string }) {
  return request.post({
    url: `/platform/menuMoveDowngrade`,
    headers: {
      'Content-Type': 'application/x-www-form-urlencoded',
    },
    data: params,
  });
}

/**
 * 菜单升级
 * @param params
 * @returns
 */
export function menuMoveUpgrade(params: { id: string }) {
  return request.post({
    url: `/platform/menuMoveUpgrade`,
    headers: {
      'Content-Type': 'application/x-www-form-urlencoded',
    },
    data: params,
  });
}

/**
 * 菜单上移
 * @param params
 * @returns
 */
export function menuMoveUp(params: { id: string }) {
  return request.post({
    url: `/platform/menuMoveUp`,
    headers: {
      'Content-Type': 'application/x-www-form-urlencoded',
    },
    data: params,
  });
}

/**
 * 菜单下移
 * @param params
 * @returns
 */
export function menuMoveDown(params: { id: string }) {
  return request.post({
    url: `/platform/menuMoveDown`,
    headers: {
      'Content-Type': 'application/x-www-form-urlencoded',
    },
    data: params,
  });
}

/**
 * 获取单条菜单信息
 * @param params
 * @returns
 */
export function getSingleMenu(params: { id: string }) {
  return request.get<MenuInfo>({
    url: '/platform/menuGet',
    params,
  });
}

/**
 * 增加节点
 * @param type 节点类型
 * @param params
 * @returns
 */
export function addMenu(type: 'root' | 'child' | 'upper' | 'below', params: AddParams) {
  console.log(type, params);
  const map = new Map([
    ['root', 'menuAddSon'],
    ['child', 'menuAddSon'],
    ['upper', 'menuAddBefore'],
    ['below', 'menuAddAfter'],
  ]);
  const url = map.get(type);
  return request.post({
    url: `/platform/${url}`,
    data: params,
  });
}

/**
 * 更新单条数据
 * @param params
 * @returns
 */
export function editorMenu(params: AddParams) {
  return request.post({
    url: `/platform/menuUpdate`,
    data: params,
  });
}
